package com.iot.inclinometer.util;

import com.iot.inclinometer.pojo.DataGroupRes;
import com.iot.inclinometer.pojo.DataRes;
import com.iot.inclinometer.pojo.UploadDataInfo;
import com.iot.inclinometer.pojo.UploadRes;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Song Yu
 * @Date: 2021/4/15 0015 10:13
 */
public class BeanUtil {
    public static void translate(List<UploadDataInfo> infoList, UploadRes res){
        res.setProjectName(infoList.get(0).getProjectName())
                .setWorkPointName(infoList.get(0).getWorkpointName())
                .setMonitorPointName(infoList.get(0).getDrillNo())
                .setDataGroup(new LinkedList<>());
        for(UploadDataInfo item :infoList){
            DataRes dataRes = new DataRes();
            dataRes.setDepth(Double.parseDouble(item.getDepth()))
                    .setRelativeA0(Double.parseDouble(item.getLx())*10)
                    .setRelativeB0(Double.parseDouble(item.getLy())*10)
                    .setRelativeSynthesis(Double.parseDouble(item.getError())*10);
            if(res.getDataGroup().isEmpty()||noContainsTime(res.getDataGroup(),item.getTime())){
                DataGroupRes dataGroup = new DataGroupRes();
                dataGroup.setCreateTime(item.getTime());
                dataGroup.setData(new LinkedList<>());
                dataGroup.getData().add(dataRes);
                res.getDataGroup().add(dataGroup);
            }else{
                res.getDataGroup().get(res.getDataGroup().size()-1).getData().add(dataRes);
            }
        }
    }

    public static boolean noContainsTime(List<DataGroupRes> res,String time){
        for(DataGroupRes item:res){
            if(item.getCreateTime().equals(time)){
               return false;
            }
        }
        return true;
    }
    public static void mathData(List<DataGroupRes> groupList, List<UploadDataInfo> lastList){
        List<DataRes> last = new ArrayList<>();
        String time = "";
        if(!lastList.isEmpty()) {
            for (UploadDataInfo i : lastList) {
                DataRes j = new DataRes();
                j.setDepth(Double.parseDouble(i.getDepth()))
                        .setRelativeA0(Double.parseDouble(i.getLx()))
                        .setRelativeB0(Double.parseDouble(i.getLy()))
                        .setRelativeSynthesis(Double.parseDouble(i.getError()));
                last.add(j);
            }
            time = lastList.get(0).getTime();
        }
        for(DataGroupRes group:groupList){
                for (DataRes item : group.getData()) {
                    if(last.isEmpty()){
                        item.setSpeedSynthesis(0D)
                                .setSpeedA0(0D)
                                .setSpeedB0(0D);
                        System.out.println("haha");
                    }else {
                    DataRes lastItem = getLastData(last, item);
                    item.setSpeedSynthesis((item.getRelativeSynthesis() - lastItem.getRelativeSynthesis())*1000*60*60 / mathTime((group.getCreateTime()), time))
                            .setSpeedA0((item.getRelativeA0() - lastItem.getRelativeA0())*1000*60*60 / mathTime((group.getCreateTime()), time))
                            .setSpeedB0((item.getRelativeB0() - lastItem.getRelativeB0())*1000*60*60 / mathTime((group.getCreateTime()), time));
//                        System.out.println("lastA0="+lastItem.getRelativeA0()+";lastB0="+lastItem.getRelativeB0()+";lastAB="+lastItem.getRelativeSynthesis()+";time="+mathTime((group.getCreateTime()), time));
                    }
                }
            last=group.getData();
            time=group.getCreateTime();
        }
    }

    private static long mathTime(String endTime,String startTime){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置要读取的时间字符串格式
        Date endDate = null;
        Date startDate = null;
        try {
            endDate = format.parse(endTime);
            startDate = format.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //转换为Date类
        Long end=endDate.getTime();
        Long start=startDate.getTime();
        return end-start;
    }

    private static DataRes getLastData(List<DataRes> last,DataRes item){
        for(DataRes i:last){
            if(i.getDepth().equals(item.getDepth())){
                return i;
            }
        }
        return null;
    }
}
